<template>
    <view id="loginwx">
		<Header txt="登录关联" v-if="isreslis==true&&baidu==true" > </Header>
		<Header txt="登录"  v-if="isreslis==false&&baidu==true" ></Header>
		<HeaderB txt="登录"  v-if="isreslis==false&&baidu==false"></HeaderB>
		<view class="login">
			<view class="" v-if="isreslis==true">
				<view class="top-tit">
					登录关联
					<view class="tit">请验证需要关联的熊猫账号</view>
				</view>
				<view>		
					<input v-model="loginForm.account" placeholder="请输入手机号" autocomplete="off" />
				</view>
				<view>		
					<input type="password" password=true v-model="loginForm.password"  placeholder="请输入密码"  />
				</view>
				<label>
					<text class="pass" @click="gofo">忘记密码?</text>
				</label>
				<view  id="captcha1">	
				</view>
				<view class="">
					<button @click="appLoginWx" class="loginbtn">微信关联登录</button>
				</view>	
				<button @click="gopage" class="registerbtn">去注册关联</button>
			</view>
			<view class="loginwxBig" v-if="isreslis==false">
				<view class="loginwxTxt">
					<text class="text">熊猫代理</text>
				</view>
			</view>
			<view class="login-box" v-if="isreslis==false">
				<button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" class="wxlogin">
					<image class="button-img" src="../../static/other/wx.png"></image>
					微信用户一键登录</button>
				</button>
			
				<view class="gotext" @click="gopage2">手机号登录/注册</view>
			</view>
		</view>
    </view>
</template>
<script>
	import WXBizDataCrypt from '../../common/WXBizDataCrypt.js';
	export default {
	    data() {
	      return {
	        sessionKey: '',
	        openId: '',
	        nickName: null,
	        avatarUrl: null,
			userInfo:{},
			data:{},
			loginForm: {
			        account: "",
			        password: "",
			        remember: true,
					unionId:'',
			    },
			isreslis:false,
			canIUseGetUserProfile: false,
			weixinCode:'',
			appid:'',
			baidu:true
	      };
	    },
		onLoad() {
			// var _this = this;
			//console.log(uni.getUserProfile);
			// if( uni.getUserProfile ){  
			//     this.canIGetUserProfile = true;  
			// }  
			// uni.login({
			//       provider: 'weixin',
			//       scopes: 'auth_base',
			//       success: loginRes => {
			//         this.weixinCode = loginRes.code;
			// 		uniCloud.callFunction({
			// 		    name: 'user-wx',
			// 			data:{
			// 				code:loginRes.code
			// 			},
			// 			success: function (res2) {
			// 				console.log("解密数据",res2);
			// 				uni.setStorageSync("unionidWx",res2.result.unionid)
			// 				uni.setStorageSync("sessionKeywx",res2.result.sessionKey)
			// 			}
			// 		})
			//       },
			//       fail: res => {
			//         console.log('login fail', res);
			//       }
			//     });
		},
		onShow() {
			// #ifdef MP-BAIDU
			this.baidu=false
			// #endif
		},
	    methods: {
			appLoginWx(){
				let that=this
				let _params
				  _params = {
				    unionId: uni.getStorageSync("unioidwx"),
				    account: this.loginForm.account,
				    password: this.loginForm.password,
				  };  
				this.$store
				  .dispatch("RelateAccount",_params)
				  .then((response) => {
					console.log("122",response.data);
					if(response.data.code==0){
						wx.login({
						  success: function (res3) {
								that.callBack(res3.code)
							}
						})
						
					}
				  })
				  .catch(() => {}); 
			},
			gofo(){
				uni.navigateTo({
					url:"/pages/login/forgetPas"
				})
			},
			gopage(){
				uni.navigateTo({
					url:"/pagesB/loginwx/registerwx"
				})
			},
			gopage2(){
				console.log(111);
				uni.navigateTo({
					url:"/pages/login/login"
				})
			},
			onGetPhoneNumber(e){  
				let that = this
			    if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
			    	//用户决绝授权  
			        //拒绝授权后弹出一些提示  
			    }else{//允许授权
					// let AppId = this.appid
					// let sessionKey=uni.getStorageSync("sessionKeywx")
					// let pc = new WXBizDataCrypt(AppId, sessionKey)
					// let encryptedData=e.detail.encryptedData  //加密的用户信息  
					// let iv= e.detail.iv  //加密算法的初始向量  时要用到
					// console.log(AppId,sessionKey,encryptedData,iv);
					// let data = pc.decryptData(encryptedData , iv)
					// console.log('解密后 手机号: ', data.phoneNumber)
					wx.login({    //调用登录接口，获取 code
					  success: function (res3) {
							that.callBack(res3.code)
						}
					})
					return
			    }      
			},
			blank(){
				uni.navigateTo({
					url: 'https://xiongmaodaili.com/agreement/', //
					success: function() {
					}, //成功后的回调；
				})	
			},
			goPage(){
			    uni.navigateTo({
			    	url:"/pages/login/login"
			    })
			},
			callBack(code){
				let that=this
				uni.hideLoading()
				let _params
				  _params = {
				    code:code,
				  };  
				this.$store
				  .dispatch("CallBack",_params)
				  .then((response) => {
					  let data=response.data
					if(response.data.code==0){
						if(data.obj&&data.obj.unionid.length>1){
							console.log('欧锦',response);
							uni.setStorageSync("unioidwx",data.obj.unionid)
							uni.showModal({
							    title: '登录失败',
							    content: '该微信账号暂未关联熊猫代理账号无法直接登录',
								cancelText:"去注册",
								confirmText:'去关联',
							    success: function (res) {
									if (res.confirm) {
										console.log('用户点击去关联');
										that.isreslis=true
									} else if (res.cancel) {
										console.log('用户点击去注册');
										uni.navigateTo({
											url:"/pagesB/loginwx/registerwx"
										})
									}
								}
							})
						}else{
							uni.showModal({
							    title: '登陆成功',
							    content: '欢迎回来，尊敬的客户',
							    success: function (res) {
									uni.setStorageSync('Cookie', response.header['Set-Cookie']);
									uni.login({
										success: (res) => {
											// 客户端代码
											console.log(res);
											uniCloud.callFunction({
											    name: 'user-wx',
												data:{
													code:res.code
												},
											    success(res){
													let data={
														id:res.result.openid
													}
													uni.setStorageSync('wxId',data)			 
											    },
											    fail(res){
													console.log("失败",res);
													uni.showToast({
														title:"失败"+res
													})
											    }
											})						
										}
									})
									uni.switchTab({
										url: "/pages/user/user"
									})
								}
							})
						}
					}
				  })
				  .catch(() => {}); 
			},
			//登录参数
			getCodeUrl(code,state) {
				let that=this
				this.$store
				  .dispatch("GetCodeUrl")
				  .then((response) => {
					if(response.data.code=='0'){
						that.data.state=response.data.obj.state
						that.appid=response.data.obj.appId
						console.log(222,response.data.obj.appId);
					}
				  })
				  .catch(() => {}); 
			},	
	    }
	}
</script>
<style lang="less">
	#loginwx{
		width: 90%;
		margin: 0 auto;
		.pass{
			margin-left: 30rpx;			
			font-size: 30rpx;
			color: #9A9A9A;
			cursor: pointer;
		}
		.loginbtn{
			margin: 0 auto;
			background-color: rgb(72, 110, 245);
			color: rgb(255, 255, 255);
			border-radius: 100rpx;
			margin-top: 80rpx;
		}
		.loginbtn2{
			width: 300rpx;
			height: 66rpx;
			line-height: 66rpx;
			background-color: rgb(255, 255, 255);
			border: 2rpx solid #cccccc;
			color: rgb(153, 153, 153);
			margin-top: 30rpx;
		}
		.registerbtn{
			margin: 0 auto;
			background-color: rgb(255, 255, 255);
			color: rgb(153, 153, 153);
			border: 2rpx solid #cccccc;
			margin-top: 70rpx;
			width: 200rpx;
			font-size: 24rpx;
			border-radius: 100rpx;
		}
		.top-tit{
			font-size: 60rpx;
			color: #222222;
			font-weight: bold;
			margin-top: 80rpx;
			.tit{
				font-size: 36rpx;
				padding-top: 20rpx;
				margin-bottom: 100rpx;
			}    
		}
		input {
			width: 100%;
			height: 80rpx;
			margin-bottom: 10rpx;
			border-radius: 80rpx;
			background-color: #f4f4f4;
			color: #525252;
			padding-left: 20rpx;
			margin-bottom: 50rpx;
		}
		.span1 {
		      // float: left;
		      width: 14px;
			  height: 14px;
		      cursor: pointer;
		      color: #3373fc;
		      &:hover {
		        opacity: 0.8;
		      }
			  
		 }
		uni-checkbox .uni-checkbox-input {
			width: 14px;
			height: 14px;
		}
		button{
			margin-top: 30rpx;
			margin-bottom: 30rpx;
		}
		button::after{
			border:none;
		}
 		.login-box{
			width: 90%;
			margin: 0 auto;
			// margin-top: 65%;
		}
		.loginwxBig{
			width: 100%;
			height: 300rpx;
			margin-top:150rpx;
			margin-bottom:150rpx;
			.loginwxTxt{
				background: url('https://iconimg.oss-cn-hangzhou.aliyuncs.com/%E7%86%8A%E7%8C%AB%E4%BB%A3%E7%90%86/%E5%9B%BE%E6%A0%87/loginwx.png') no-repeat center center;
				background-size: 80px;
				height: 300rpx;
				font-size: 18px;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #333333;
				text-align: center;
				padding: top;
				position: relative;
			}
			.text{
				width: 100%;
				position: absolute;
				bottom: 20rpx;
				left: 0;
			}
			// background:#007AFF;
		}
		.gotext{
			margin-top: 36rpx;
			text-align: center;
			font-size: 36rpx;
			font-weight: 400;
			color: #333333;
		}
		button{
			background: #007AFF;
			border-radius: 10px;
			color: #FFFFFF;
		}
		.wxlogin{
			background: #2C75F1;
			border-radius: 10px;
			height: 92rpx;
			font-size: 32rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 92rpx;
			overflow: hidden;
			image{
				width: 38rpx;
				height: 32rpx;
				position: relative;
				margin-right: 10rpx;
				vertical-align: middle;
				// top: 10rpx;
			}
		}
	}
</style>